import {
  plans,
  joinPlan,
  createunifiedorder
} from '../../api/index.js'

Page({
  data: {
    show: false,
    planId: '',
    plan: '',
    url: wx.getStorageSync('url'),
    clik:false,
    days:null
  },

  jz(){

    wx.navigateToMiniProgram({
      appId: 'wx58238518b3ce2a49',
      path: 'packages/goods/detail/index?kdt_id=40752081&is_share=1&bannerId=f.71059898~goods.6~1~ALZHeu4T&share_cmpt=native_wechat&alias=2xmoh2qeh7hg1&sl=3ELM3b&oid=0&withSeckillError=true&from_uuid=E0dNdJhLPdeVyYR1588555751003'
    })
     
  },
  ysj() {
    wx.navigateToMiniProgram({
      appId: 'wx58238518b3ce2a49',
      path: 'packages/goods/detail/index?kdt_id=40752081&is_share=1&share_cmpt=native_wechat&alias=3ez8dwpskfl8h&sl=3ELM3b&oid=0&withSeckillError=true&from_uuid=E0dNdJhLPdeVyYR1588555751003'
    })
  },
  yj() {
    wx.navigateToMiniProgram({
      appId: 'wx58238518b3ce2a49',
      path: 'packages/goods/detail/index?kdt_id=40752081&is_share=1&bannerId=f.71059898~goods.5~4~ALZHeu4T&share_cmpt=native_wechat&alias=3f5cjy0lug1gh&sl=3ELM3b&oid=0&withSeckillError=true&from_uuid=E0dNdJhLPdeVyYR1588555751003'
    })
  },

  onLoad: function(options) {
    this.setData({
      planId: options.planId
    })
    this.plan(options.planId)
  },

  plan(planId) {
    plans(planId).then(res => {
      this.setData({
        plan: res,
        days: res.days
      })
      console.log(this.data.days,'111111')
    })
  },

  onClickShow() {
    this.setData({
      show: true
    })
  },
  onClickHide() {
    if(this.data.clik){
      wx.navigateTo({
        url: '/pages/schedule/schedule',
      })
    }
    this.setData({
      show: false
    });
  },


  addPlan() {
    let that=this
    let data = {
      planId: this.data.planId
    }
    joinPlan(data).then(res => {
      if (res.errcode == '00000') {
        let userPlanId = res.userPlanId
        if (res.price == 0) {
          wx.navigateTo({
            url: '/pages/course/course',
          })
        } else if (res.price > 0) {
          let data = {
            userPlanId: userPlanId
          }
          createunifiedorder(data).then(res => {
            wx.requestPayment({
              timeStamp: res.timeStamp,
              nonceStr: res.nonceStr,
              package: res.package,
              signType: res.signType,
              paySign: res.paySign,
              success(res) {
                if (res.errMsg == 'requestPayment:ok') {
                  if (that.data.days == 14 || that.data.days==28){
                    that.setData({
                      show:true,
                      clik:true
                    })
                  }else{
                    wx.navigateTo({
                      url: '/pages/schedule/schedule',
                    })
                  }
            
                } else {
                  wx.showToast({
                    title: '支付失败，请重新登录后再次尝试',
                    icon: 'none'
                  })
                  wx.clearStorage({
                    complete(res) {
                      wx.navigateTo({
                        url: '/pages/home/home',
                      })
                    }
                  });
                }
              },
              fail(res) {
                wx.showToast({
                  title: '支付失败，请重新登录后再次尝试',
                  icon: 'none'
                })
                wx.clearStorage({
                  complete(res) {
                    wx.navigateTo({
                      url: '/pages/home/home',
                    })
                  }
                });
              }
            })
          })
        }
      }
    })
  },
  //分享
  onShareAppMessage: function () {
    return {
      title: '我正在使用SMART睡眠训练计划，超有用！推荐给你',
      imageUrl: '../../images/98875.png'

    }
  },

})